package com.yy.project.controller;

import com.yy.project.entity.Task;
import com.yy.project.service.TaskService;
import lombok.extern.slf4j.Slf4j;
import org.noear.solon.annotation.*;
import org.noear.solon.core.handle.Context;
import org.noear.solon.core.handle.MethodType;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
@Controller
@Mapping("/api/tasks")
public class TaskController {
    
    @Inject
    private TaskService taskService;
    
    // 跨域配置
    @Mapping(value = "/**", method = {MethodType.OPTIONS})
    public void options(Context ctx) {
        ctx.headerSet("Access-Control-Allow-Origin", "*");
        ctx.headerSet("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE, OPTIONS");
        ctx.headerSet("Access-Control-Allow-Headers", "Content-Type, Authorization");
        ctx.status(200);
    }
    
    /*@Before
    public void before(Context ctx) {
        ctx.headerSet("Access-Control-Allow-Origin", "*");
        ctx.headerSet("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE, OPTIONS");
        ctx.headerSet("Access-Control-Allow-Headers", "Content-Type, Authorization");
    }*/
    
    @Get
    @Mapping("/")
    public Map<String, Object> getAllTasks() {
        Map<String, Object> result = new HashMap<>();
        try {
            List<Task> tasks = taskService.getAllTasks();
            result.put("code", 200);
            result.put("message", "获取任务列表成功");
            result.put("data", tasks);
        } catch (Exception e) {
            log.error(e.getMessage() , e);
            result.put("code", 500);
            result.put("message", "获取任务列表失败: " + e.getMessage());
        }
        return result;
    }
    
    @Get
    @Mapping("/{id}")
    public Map<String, Object> getTaskById(Long id) {
        Map<String, Object> result = new HashMap<>();
        try {
            Task task = taskService.getTaskById(id);
            if (task != null) {
                result.put("code", 200);
                result.put("message", "获取任务成功");
                result.put("data", task);
            } else {
                result.put("code", 404);
                result.put("message", "任务不存在");
            }
        } catch (Exception e) {
            result.put("code", 500);
            result.put("message", "获取任务失败: " + e.getMessage());
        }
        return result;
    }
    
    @Post
    @Mapping("/")
    public Map<String, Object> createTask(@Body Task task) {
        Map<String, Object> result = new HashMap<>();
        try {
            Task createdTask = taskService.createTask(task);
            result.put("code", 200);
            result.put("message", "创建任务成功");
            result.put("data", createdTask);
        } catch (Exception e) {
            result.put("code", 500);
            result.put("message", "创建任务失败: " + e.getMessage());
        }
        return result;
    }
    
    @Put
    @Mapping("/{id}")
    public Map<String, Object> updateTask(Long id, @Body Task task) {
        Map<String, Object> result = new HashMap<>();
        try {
            task.setId(id);
            Task updatedTask = taskService.updateTask(task);
            result.put("code", 200);
            result.put("message", "更新任务成功");
            result.put("data", updatedTask);
        } catch (Exception e) {
            result.put("code", 500);
            result.put("message", "更新任务失败: " + e.getMessage());
        }
        return result;
    }
    
    @Delete
    @Mapping("/{id}")
    public Map<String, Object> deleteTask(Long id) {
        Map<String, Object> result = new HashMap<>();
        try {
            boolean deleted = taskService.deleteTask(id);
            if (deleted) {
                result.put("code", 200);
                result.put("message", "删除任务成功");
            } else {
                result.put("code", 404);
                result.put("message", "任务不存在");
            }
        } catch (Exception e) {
            result.put("code", 500);
            result.put("message", "删除任务失败: " + e.getMessage());
        }
        return result;
    }
    
    // 测试接口
    @Get
    @Mapping("/test")
    public Map<String, Object> test() {
        Map<String, Object> result = new HashMap<>();
        result.put("code", 200);
        result.put("message", "后端服务正常运行");
        result.put("timestamp", System.currentTimeMillis());
        return result;
    }
}